package com.qkbb.admin.kuibu.qkbb.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.qkbb.admin.kuibu.R;
import com.qkbb.admin.kuibu.qkbb.funcation.SDCardHelper;
import com.qkbb.admin.kuibu.qkbb.view.TitleBarView;

public class Modification extends Activity {
    private TitleBarView titleBarView;
    private EditText editText;
    private Button button;
    private String uri;
    private String str;
    private String data;
    private String type;
    private SharedPreferences.Editor editor;
    private SharedPreferences sharedPreferences;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_modification);
        titleBarView= (TitleBarView) findViewById(R.id.modification_text);
        editText= (EditText) findViewById(R.id.modification_edit);
        button= (Button) findViewById(R.id.modification_button);
        str=getIntent().getStringExtra("text");
        uri=getIntent().getStringExtra("uri");
        type=getIntent().getStringExtra("type");
        titleBarView.setCenterTexiView(str);
        titleBarView.setColor(R.color.green);
        titleBarView.settextColor(Color.WHITE);
        titleBarView.setLeftButton(R.mipmap.fanhui_04);
        titleBarView.setLeftButtonOnclick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        final RequestQueue queue= Volley.newRequestQueue(getApplicationContext());
        sharedPreferences = getSharedPreferences("user", Context.MODE_PRIVATE);
        editor=sharedPreferences.edit();
        
        final String user_token= SDCardHelper.loadFileFromSdCard("user_token",getApplicationContext());
        Log.e("user_token", user_token);

        button.setOnClickListener(new View.OnClickListener() {

            @Override
             public void onClick(View v) {
                data=editText.getText().toString();
                RequestParams params=new RequestParams();
                Log.e("data", data);
                editor.putString(type,data);
                editor.commit();
                if (type.equals("gender")){
                    if (data.equals("男")){
                        data="1";
                    }else if (data.equals("女")){
                        data="2";
                    }else {
                        Toast.makeText(getApplicationContext(),"输入有误",Toast.LENGTH_SHORT).show();
                    }
                }
                params.addBodyParameter(type,data);

                Log.e("type", type);
              //  params.addHeader("nickname", data);
                //params.addQueryStringParameter("nickname",data);
                HttpUtils utils=new HttpUtils();
                utils.configCurrentHttpCacheExpiry(1000);
                Log.e("uri",uri+user_token);
                utils.send(HttpRequest.HttpMethod.PUT, uri+user_token, params, new RequestCallBack<String>() {
                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {
                        Log.e("fshsaj", responseInfo.result);
                        Toast.makeText(getApplicationContext(), "修改成功", Toast.LENGTH_SHORT).show();
                        Intent intent=new Intent();
                        intent.putExtra(type,data);
                        setResult(RESULT_OK,intent);
                        finish();
                    }

                    @Override
                    public void onFailure(HttpException e, String s) {

                    }
                });





//                data=editText.getText().toString();
//                StringRequest request=new StringRequest(Request.Method.PUT, uri + user_token, new Response.Listener<String>() {
//                    @Override
//                    public void onResponse(String s) {
//                        Log.e("String",s);
//                        Toast.makeText(getApplicationContext(),"修改成功",Toast.LENGTH_SHORT).show();
//
//                    }
//                }, new Response.ErrorListener() {
//                    @Override
//                    public void onErrorResponse(VolleyError volleyError) {
//                        Toast.makeText(getApplicationContext(),"修改失败",Toast.LENGTH_SHORT).show();
//                    }
//                }
//
//                ) {
//                    public int sex;
//
//                    @Override
//                    protected Map<String, String> getParams() throws AuthFailureError {
//                        Map<String,String> map=new HashMap<String, String>();
//                        switch (str){
//                            case "昵称":
//                                map.put("nickname",data);
//                                Log.e("nicknme", data);
//                            break;
//                            case "性别":
//                                if (data.equals("男")){
//                                    sex = 1;
//                                }else if (data.equals("女")){
//                                    sex=2;
//                                }
//                                map.put("gender",sex+"");
//                            break;
//                            case "地区":
//                                map.put("cityid",data);
//                                break;
//                            case "签名":
//                                map.put("signature",data);
//                                break;
//                        }
//                        Log.e("nickname", data);
//                        return map;
//                    }
//                };
//                queue.add(request);
            }
        });

    }
}
